<?php
include_once(dirname(__FILE__).'/log4php/Logger.php');
Logger::configure('log4php.xml');
$logger = Logger::getLogger('test');
$logger->info("begin udp.php");

//'192.168.1.110 和 '127.0.0.1'都可以
//$server_ip = '192.168.1.110';
$server_ip = '127.0.0.1';
$server_port = 3456;

$str_sep = "\n\n\n\n\n";

$content = $_POST["textContent"];
$expression = $_POST["textExpression"];
$msg = "$content$str_sep$expression";
$len = strlen($msg);

//echo $msg;

$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($sock == FALSE)
{
    $logger->error("socket create error:".socket_strerror(socket_last_error()));
    return 1;
}

$send_len = socket_sendto($sock, $msg, $len, 0, $server_ip, $server_port);
if ($send_len == false)
{
    $logger->error("socket send error");
    return 1;
}
$logger->debug("socket send len: ".$send_len);

$recv_len = socket_recvfrom($sock, $buf,1024, 0, $server_ip, $server_port);
if ($recv_len == false)
{
    $logger->error("socket recv error:".socket_strerror(socket_last_error()));
    return 1;
}
$logger->debug("socket recv len: ".$recv_len);
socket_close($sock);
echo $buf;

$logger->debug("upd end");
?>
